﻿using System.Collections.Generic;
using System.Net;
using System.Linq;
using WMI.root.cimv2;
using System.Management;
using System;

public class NetworkAdapterWrapper
{
    public string DeviceID { get; set; }
    public string Type { get; set; }
    public string MACAddress { get; set; }
    public string Manufacturer { get; set; }
    public string Name { get; set; }
    public string NetworkConnectionID { get; set; }
    public NetworkAdapter.NetConnectionStatusValues NetworkConnectionStatus { get; set; }
    public Dictionary<IPAddress,IPAddress> Addresses { get; set; }
    public bool DHCPEnabled { get; set; }
    public IPAddress DHCPServer { get; set; }
    public DateTime DHCPLeaseObtained { get; set; }
    public DateTime DHCPLeaseExpires { get; set; }
    public IPAddress[] Gateway { get; set; }
    public IPAddress[] DNSServers { get; set; }
}

public class NetworkAdaptersWrapper
{
    public IEnumerable<NetworkAdapterWrapper> Adapters
    {
        get
        {
            var adapters = NetworkAdapter.GetInstances("NetConnectionId is not null");
            foreach (NetworkAdapter adapter in adapters)
            {
                yield return GetAdapter(adapter);
            }                   
        }
    }

    public NetworkAdapterWrapper GetAdapter(NetworkAdapter adapter)
    {        
            var q = new ManagementObjectSearcher(string.Format("ASSOCIATORS OF {{{0}}} WHERE ResultClass=Win32_NetworkAdapterConfiguration", adapter.Path.ToString()));
            NetworkAdapterConfiguration conf = null;
            foreach (var tmp in q.Get())
            {
                conf = new NetworkAdapterConfiguration(tmp);
            }

            return new NetworkAdapterWrapper()
            {
                DeviceID = adapter.DeviceID,

                Type = adapter.AdapterType,

                NetworkConnectionID = adapter.NetConnectionID,
                NetworkConnectionStatus = adapter.NetConnectionStatus,

                Manufacturer = adapter.Manufacturer,
                Name = adapter.Description,

                MACAddress = adapter.MACAddress,
                Addresses = CombineIPAndMask(conf.IPAddress, conf.IPSubnet),
                Gateway = Others.StringArrayToIPArray(conf.DefaultIPGateway),

                DNSServers = Others.StringArrayToIPArray(conf.DNSServerSearchOrder),

                DHCPEnabled = conf.DHCPEnabled,
                DHCPServer = conf.DHCPServer != null ? IPAddress.Parse(conf.DHCPServer) : null,
                DHCPLeaseObtained = conf.DHCPLeaseObtained,
                DHCPLeaseExpires = conf.DHCPLeaseExpires,
            };        
    }

    public NetworkAdapterWrapper GetAdapter(string deviceID)
    {
        var adapters = NetworkAdapter.GetInstances("DeviceId=" + deviceID);
        foreach (NetworkAdapter item in adapters)
        {
            return GetAdapter(item);
        }
        throw new ApplicationException();
    }

    private Dictionary<IPAddress, IPAddress> CombineIPAndMask(string[] ips, string[] masks)
    {
        var res = new Dictionary<IPAddress, IPAddress>();

        if ((ips == null) || (masks == null))
            return res;

        for (int i = 0; i < ips.Length; i++)
        {
            var ip = IPAddress.Parse(ips[i]);
            var mask = IPAddress.Parse(masks[i]);

            res.Add(ip, mask);
        }
        
        return res;
    }
}
